package com.ruoyi.oss.entity.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.oss.model.PlatformConfig;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PlatformConfigJsonTypeHandler extends BaseTypeHandler<PlatformConfig> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, PlatformConfig o, JdbcType jdbcType) throws SQLException {
        if(o!=null){
            preparedStatement.setString(i, JSON.toJSONString(o, SerializerFeature.WriteClassName));
        }else {
            preparedStatement.setString(i, null);
        }

    }

    @Override
    public PlatformConfig getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        String sqlJson = resultSet.getString(columnName);
        if (null != sqlJson) {
            return JSON.parseObject(sqlJson,PlatformConfig.class, Feature.DisableSpecialKeyDetect);
        }
        return null;
    }

    @Override
    public PlatformConfig getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String sqlJson = resultSet.getString(i);
        if (null != sqlJson) {
            return JSON.parseObject(sqlJson,PlatformConfig.class, Feature.DisableSpecialKeyDetect);
        }
        return null;
    }

    @Override
    public PlatformConfig getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String sqlJson = callableStatement.getString(i);
        if (null != sqlJson) {
            return JSON.parseObject(sqlJson,PlatformConfig.class, Feature.DisableSpecialKeyDetect);
        }
        return null;
    }
}
